package com.blazingredfireball.rouleurlog.ria.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.ria.business.RouteDelegate;
	import com.blazingredfireball.rouleurlog.ria.event.CreateRouteEvent;
	import com.blazingredfireball.rouleurlog.ria.model.RouleurlogModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class CreateRouteCommand extends BaseCommand implements ICommand, IResponder
	{
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute(event:CairngormEvent):void
		{
		    var delegate : RouteDelegate = new RouteDelegate( this );    
		    delegate.createRoute(CreateRouteEvent(event).route);
		}
		
		public function result(data:Object):void
		{
			RouleurlogModelLocator.getInstance().routes.addItem( data.result );
		}
		
		public function fault(info:Object):void
		{
			var faultEvent :FaultEvent = FaultEvent( info );
			
			if (!checkForAuthenticationFault(faultEvent))
		    {
		    	Alert.show("Route could not be created!");
		    }
		}
	}
}